<html>
<head><meta charset="utf-8"><title>introduce a `Visit` trait to go with `Fold` #333 · wg-traits · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/index.html">wg-traits</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333.html">introduce a `Visit` trait to go with `Fold` #333</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="191673352"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/introduce%20a%20%60Visit%60%20trait%20to%20go%20with%20%60Fold%60%20%23333/near/191673352" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jack Huey <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333.html#191673352">(Mar 24 2020 at 21:15)</a>:</h4>
<p><span class="user-mention" data-user-id="251766">@Areredify</span> I'm going to open this topic because I (or <span class="user-mention" data-user-id="116009">@nikomatsakis</span>) need to reply to your issue comment</p>



<a name="191673420"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/introduce%20a%20%60Visit%60%20trait%20to%20go%20with%20%60Fold%60%20%23333/near/191673420" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jack Huey <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333.html#191673420">(Mar 24 2020 at 21:15)</a>:</h4>
<p>I looked at it, telling myself I was going to think about it and get back to it and forgot</p>



<a name="191673438"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/introduce%20a%20%60Visit%60%20trait%20to%20go%20with%20%60Fold%60%20%23333/near/191673438" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jack Huey <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333.html#191673438">(Mar 24 2020 at 21:16)</a>:</h4>
<p>so this is the reminder to myself</p>



<a name="191675270"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/introduce%20a%20%60Visit%60%20trait%20to%20go%20with%20%60Fold%60%20%23333/near/191675270" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333.html#191675270">(Mar 24 2020 at 21:31)</a>:</h4>
<p>So, <span class="user-mention" data-user-id="251766">@Areredify</span>, to me the primary benefit of passing values explicitly instead of using a mutable value is that mutable values tend to "leak" and create unclear dataflow. Using the return values means that we explicitly compute values from the leaves on up, and that all "combination" of values occurs at a clear spot. If you use a mutable field, then when you read that field, you wind up getting a value that is some combination of the previous values, and not necessarily determined based on the things below you in the tree. Overall, I just find the result less predictable and a bit harder to follow, myself.</p>



<a name="191675318"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/introduce%20a%20%60Visit%60%20trait%20to%20go%20with%20%60Fold%60%20%23333/near/191675318" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333.html#191675318">(Mar 24 2020 at 21:31)</a>:</h4>
<p>In rustc's visitor, the design is specialized to finding things -- it always propagates a boolean up, and it's hard-coded to stop when that flag returns true</p>



<a name="191675492"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/introduce%20a%20%60Visit%60%20trait%20to%20go%20with%20%60Fold%60%20%23333/near/191675492" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333.html#191675492">(Mar 24 2020 at 21:33)</a>:</h4>
<p>To continue, the point around mutable state, it feels like -- very often -- when I write a visitor of this kind, I wind up having to write code that does something like</p>
<div class="codehilite"><pre><span></span><span class="kd">let</span><span class="w"> </span><span class="n">old_value</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="n">value</span><span class="p">;</span><span class="w"></span>
<span class="bp">self</span><span class="p">.</span><span class="n">value</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">new_value</span><span class="p">;</span><span class="w"></span>
<span class="n">super_visit</span><span class="p">();</span><span class="w"></span>
<span class="bp">self</span><span class="p">.</span><span class="n">value</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">old_value</span><span class="p">;</span><span class="w"></span>
</pre></div>


<p>and that always just seems really fragile compared to</p>
<div class="codehilite"><pre><span></span><span class="n">super_visit</span><span class="p">(</span><span class="n">new_value</span><span class="p">);</span><span class="w"></span>
</pre></div>



<a name="191675505"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/introduce%20a%20%60Visit%60%20trait%20to%20go%20with%20%60Fold%60%20%23333/near/191675505" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333.html#191675505">(Mar 24 2020 at 21:33)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116009">nikomatsakis</span> <a href="#narrow/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333/near/191675318" title="#narrow/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333/near/191675318">said</a>:</p>
<blockquote>
<p>In rustc's visitor, the design is specialized to finding things -- it always propagates a boolean up, and it's hard-coded to stop when that flag returns true</p>
</blockquote>
<p>one other thing is that sometimes I wind up making things too crazy generic :)</p>



<a name="191675521"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/introduce%20a%20%60Visit%60%20trait%20to%20go%20with%20%60Fold%60%20%23333/near/191675521" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333.html#191675521">(Mar 24 2020 at 21:33)</a>:</h4>
<p>maybe it'd be better to just make a <code>Find</code> trait, instead of <code>Visit</code></p>



<a name="191675532"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/introduce%20a%20%60Visit%60%20trait%20to%20go%20with%20%60Fold%60%20%23333/near/191675532" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333.html#191675532">(Mar 24 2020 at 21:33)</a>:</h4>
<p>and then we could make a <code>Count</code> trait if we wanted</p>



<a name="191675613"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/introduce%20a%20%60Visit%60%20trait%20to%20go%20with%20%60Fold%60%20%23333/near/191675613" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333.html#191675613">(Mar 24 2020 at 21:34)</a>:</h4>
<p>instead of having one derive and a kind of complex generic thing that's hard to understand, in other words, maybe better to just have more derives.</p>



<a name="191675617"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/introduce%20a%20%60Visit%60%20trait%20to%20go%20with%20%60Fold%60%20%23333/near/191675617" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333.html#191675617">(Mar 24 2020 at 21:34)</a>:</h4>
<p>actually the more I think about it the more I prefer that</p>



<a name="191675705"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/introduce%20a%20%60Visit%60%20trait%20to%20go%20with%20%60Fold%60%20%23333/near/191675705" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333.html#191675705">(Mar 24 2020 at 21:35)</a>:</h4>
<p>I guess it depends how many "custom impls" we wind up needing, though</p>



<a name="191675713"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/introduce%20a%20%60Visit%60%20trait%20to%20go%20with%20%60Fold%60%20%23333/near/191675713" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333.html#191675713">(Mar 24 2020 at 21:35)</a>:</h4>
<p>you have to reproduce the <code>Vec</code> impls and all that</p>



<a name="191675737"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/introduce%20a%20%60Visit%60%20trait%20to%20go%20with%20%60Fold%60%20%23333/near/191675737" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Areredify <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333.html#191675737">(Mar 24 2020 at 21:35)</a>:</h4>
<p>The main thing I didn't like is that not every computation can align with that design (mainly, if you collect some pieces into a vec or something)</p>



<a name="191675829"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/introduce%20a%20%60Visit%60%20trait%20to%20go%20with%20%60Fold%60%20%23333/near/191675829" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333.html#191675829">(Mar 24 2020 at 21:36)</a>:</h4>
<p>That seems ok to me, I guess.</p>



<a name="191675860"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/introduce%20a%20%60Visit%60%20trait%20to%20go%20with%20%60Fold%60%20%23333/near/191675860" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333.html#191675860">(Mar 24 2020 at 21:36)</a>:</h4>
<p>This sort of visitor is really not used for a ton of purposes</p>



<a name="191675886"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/introduce%20a%20%60Visit%60%20trait%20to%20go%20with%20%60Fold%60%20%23333/near/191675886" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333.html#191675886">(Mar 24 2020 at 21:37)</a>:</h4>
<p>but also</p>



<a name="191675890"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/introduce%20a%20%60Visit%60%20trait%20to%20go%20with%20%60Fold%60%20%23333/near/191675890" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333.html#191675890">(Mar 24 2020 at 21:37)</a>:</h4>
<p>you can of course add mutable state if you want</p>



<a name="191675901"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/introduce%20a%20%60Visit%60%20trait%20to%20go%20with%20%60Fold%60%20%23333/near/191675901" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333.html#191675901">(Mar 24 2020 at 21:37)</a>:</h4>
<p>e.g., we could have the "return value" be the <code>()</code> type</p>



<a name="191675904"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/introduce%20a%20%60Visit%60%20trait%20to%20go%20with%20%60Fold%60%20%23333/near/191675904" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333.html#191675904">(Mar 24 2020 at 21:37)</a>:</h4>
<p>and that means "visit all the things"</p>



<a name="191675927"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/introduce%20a%20%60Visit%60%20trait%20to%20go%20with%20%60Fold%60%20%23333/near/191675927" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333.html#191675927">(Mar 24 2020 at 21:37)</a>:</h4>
<p>if we ever need that</p>



<a name="191675970"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/introduce%20a%20%60Visit%60%20trait%20to%20go%20with%20%60Fold%60%20%23333/near/191675970" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333.html#191675970">(Mar 24 2020 at 21:38)</a>:</h4>
<p>in other words, the visitor <em>gets</em> an <code>&amp;mut self</code>, so it <em>can</em> make mutation</p>



<a name="191676019"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/introduce%20a%20%60Visit%60%20trait%20to%20go%20with%20%60Fold%60%20%23333/near/191676019" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333.html#191676019">(Mar 24 2020 at 21:38)</a>:</h4>
<p>it's just that it'd be nice for common patterns to not require it</p>



<a name="191676376"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/introduce%20a%20%60Visit%60%20trait%20to%20go%20with%20%60Fold%60%20%23333/near/191676376" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Areredify <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333.html#191676376">(Mar 24 2020 at 21:41)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116009">nikomatsakis</span> <a href="#narrow/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333/near/191675970" title="#narrow/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333/near/191675970">said</a>:</p>
<blockquote>
<p>in other words, the visitor <em>gets</em> an <code>&amp;mut self</code>, so it <em>can</em> make mutation</p>
</blockquote>
<p>That's how I understood it, however it allows the logic to be scattered.</p>
<p>Also, if you <code>Count</code> things, you do <code>answer = thing.visit(&amp;mut visitor)</code>, but if you <code>Collect</code> things you do <code>thing.visit(&amp;mut visitor); answer = visitor.result</code> and it doesn't feel right? I guess</p>



<a name="191676824"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/introduce%20a%20%60Visit%60%20trait%20to%20go%20with%20%60Fold%60%20%23333/near/191676824" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333.html#191676824">(Mar 24 2020 at 21:45)</a>:</h4>
<p>that doesn't bother me I guess. In practice I wouldn't do either, probably, but rather have some wrapper that constructs the visitor and invokes the visitor method</p>



<a name="191676934"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/introduce%20a%20%60Visit%60%20trait%20to%20go%20with%20%60Fold%60%20%23333/near/191676934" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333.html#191676934">(Mar 24 2020 at 21:46)</a>:</h4>
<p>usually we do things like <code>FindTypes::count(&amp;value)</code> which is like</p>
<div class="codehilite"><pre><span></span><span class="k">impl</span><span class="w"> </span><span class="n">FindTypes</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">count</span><span class="p">(</span><span class="n">value</span>: <span class="kp">&amp;</span><span class="nc">impl</span><span class="w"> </span><span class="n">Visit</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">usize</span> <span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">value</span><span class="p">.</span><span class="n">visit_with</span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="n">FindTypes</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="p">..</span><span class="w"> </span><span class="p">})</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>



<a name="191677155"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/introduce%20a%20%60Visit%60%20trait%20to%20go%20with%20%60Fold%60%20%23333/near/191677155" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333.html#191677155">(Mar 24 2020 at 21:48)</a>:</h4>
<p>anyway the truth is that I don't know if we'd ever do that</p>



<a name="191677187"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/introduce%20a%20%60Visit%60%20trait%20to%20go%20with%20%60Fold%60%20%23333/near/191677187" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333.html#191677187">(Mar 24 2020 at 21:48)</a>:</h4>
<p>I guess there is one other thing, <span class="user-mention" data-user-id="251766">@Areredify</span></p>



<a name="191677200"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/introduce%20a%20%60Visit%60%20trait%20to%20go%20with%20%60Fold%60%20%23333/near/191677200" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333.html#191677200">(Mar 24 2020 at 21:48)</a>:</h4>
<p>at least in the design we use for fold,</p>



<a name="191677216"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/introduce%20a%20%60Visit%60%20trait%20to%20go%20with%20%60Fold%60%20%23333/near/191677216" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333.html#191677216">(Mar 24 2020 at 21:48)</a>:</h4>
<p>most types implement the <code>Visit</code> trait, which gets a <code>&amp;mut dyn Visitor</code></p>



<a name="191677240"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/introduce%20a%20%60Visit%60%20trait%20to%20go%20with%20%60Fold%60%20%23333/near/191677240" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333.html#191677240">(Mar 24 2020 at 21:49)</a>:</h4>
<p>in other words, the logic to "walk" a type is <em>not</em> in the visitor itself,</p>



<a name="191677245"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/introduce%20a%20%60Visit%60%20trait%20to%20go%20with%20%60Fold%60%20%23333/near/191677245" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333.html#191677245">(Mar 24 2020 at 21:49)</a>:</h4>
<p>which only defines a few key callbacks,</p>



<a name="191677265"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/introduce%20a%20%60Visit%60%20trait%20to%20go%20with%20%60Fold%60%20%23333/near/191677265" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333.html#191677265">(Mar 24 2020 at 21:49)</a>:</h4>
<p>this means that the logic can't read the "internal state" (private fields) of the visitor</p>



<a name="191677292"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/introduce%20a%20%60Visit%60%20trait%20to%20go%20with%20%60Fold%60%20%23333/near/191677292" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333.html#191677292">(Mar 24 2020 at 21:49)</a>:</h4>
<p>so we'd have to add some kind of callback like <code>fn keep_visiting(&amp;self) -&gt; bool</code> I guess</p>



<a name="191677398"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/introduce%20a%20%60Visit%60%20trait%20to%20go%20with%20%60Fold%60%20%23333/near/191677398" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Areredify <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333.html#191677398">(Mar 24 2020 at 21:50)</a>:</h4>
<p>You are talking about rustc <code>Visit</code>, right?</p>



<a name="191677485"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/introduce%20a%20%60Visit%60%20trait%20to%20go%20with%20%60Fold%60%20%23333/near/191677485" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333.html#191677485">(Mar 24 2020 at 21:50)</a>:</h4>
<p>(Side note: really, this "visit" should be called "fold" or "reduce", I imagine, and what we now call "fold" should be called "map")</p>



<a name="191677511"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/introduce%20a%20%60Visit%60%20trait%20to%20go%20with%20%60Fold%60%20%23333/near/191677511" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333.html#191677511">(Mar 24 2020 at 21:51)</a>:</h4>
<p>At least to align better with the common operations</p>



<a name="191677541"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/introduce%20a%20%60Visit%60%20trait%20to%20go%20with%20%60Fold%60%20%23333/near/191677541" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333.html#191677541">(Mar 24 2020 at 21:51)</a>:</h4>
<p>In particular, this is not a "visitor" in the usual OO sense of the term</p>



<a name="191677566"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/introduce%20a%20%60Visit%60%20trait%20to%20go%20with%20%60Fold%60%20%23333/near/191677566" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333.html#191677566">(Mar 24 2020 at 21:51)</a>:</h4>
<p>which I think is kind of what we're comparing here</p>



<a name="191677607"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/introduce%20a%20%60Visit%60%20trait%20to%20go%20with%20%60Fold%60%20%23333/near/191677607" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Areredify <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333.html#191677607">(Mar 24 2020 at 21:51)</a>:</h4>
<p>Yeah I was confused about why chalks <code>Fold</code> was called <code>Fold</code></p>



<a name="191677678"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/introduce%20a%20%60Visit%60%20trait%20to%20go%20with%20%60Fold%60%20%23333/near/191677678" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333.html#191677678">(Mar 24 2020 at 21:52)</a>:</h4>
<p><span class="user-mention silent" data-user-id="251766">Areredify</span> <a href="#narrow/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333/near/191677398" title="#narrow/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333/near/191677398">said</a>:</p>
<blockquote>
<p>You are talking about rustc <code>Visit</code>, right?</p>
</blockquote>
<p>yeah, I guess I'm just saying that, as I proposed it, the <code>Visit</code> trait makes its decisions about whether to continue basd on the value returned to it, not by observing the internal state of the <code>visitor</code> argument, and that feels .. nicer to me.</p>



<a name="191677695"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/introduce%20a%20%60Visit%60%20trait%20to%20go%20with%20%60Fold%60%20%23333/near/191677695" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333.html#191677695">(Mar 24 2020 at 21:52)</a>:</h4>
<p><span class="user-mention silent" data-user-id="251766">Areredify</span> <a href="#narrow/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333/near/191677607" title="#narrow/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333/near/191677607">said</a>:</p>
<blockquote>
<p>Yeah I was confused about why chalks <code>Fold</code> was called <code>Fold</code></p>
</blockquote>
<p>it's following the names from rustc, which go .. way back.</p>



<a name="191677702"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/introduce%20a%20%60Visit%60%20trait%20to%20go%20with%20%60Fold%60%20%23333/near/191677702" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333.html#191677702">(Mar 24 2020 at 21:52)</a>:</h4>
<p>but both are kind of wrong.</p>



<a name="191678126"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/introduce%20a%20%60Visit%60%20trait%20to%20go%20with%20%60Fold%60%20%23333/near/191678126" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Areredify <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333.html#191678126">(Mar 24 2020 at 21:56)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116009">nikomatsakis</span> <a href="#narrow/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333/near/191677292" title="#narrow/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333/near/191677292">said</a>:</p>
<blockquote>
<p>so we'd have to add some kind of callback like <code>fn keep_visiting(&amp;self) -&gt; bool</code> I guess</p>
</blockquote>
<p>That's what I had in mind, yes</p>



<a name="191678223"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/introduce%20a%20%60Visit%60%20trait%20to%20go%20with%20%60Fold%60%20%23333/near/191678223" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Areredify <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333.html#191678223">(Mar 24 2020 at 21:57)</a>:</h4>
<p>But if I was to thing about <code>Visit</code> as folding, then I like your approach better</p>



<a name="191678282"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/introduce%20a%20%60Visit%60%20trait%20to%20go%20with%20%60Fold%60%20%23333/near/191678282" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Areredify <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333.html#191678282">(Mar 24 2020 at 21:58)</a>:</h4>
<p>I'll get to it, then <span aria-label="upside down" class="emoji emoji-1f643" role="img" title="upside down">:upside_down:</span></p>



<a name="192946395"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/introduce%20a%20%60Visit%60%20trait%20to%20go%20with%20%60Fold%60%20%23333/near/192946395" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Areredify <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333.html#192946395">(Apr 05 2020 at 07:31)</a>:</h4>
<p><span class="user-mention" data-user-id="116009">@nikomatsakis</span> I have a little question about the scope of Visit: in Fold, the callbacks are defined on the interesting parts of mapping (how to transform types and such), but I imagine Visit should have callbacks for almost everything we visit. What do you think?</p>



<a name="193093958"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/introduce%20a%20%60Visit%60%20trait%20to%20go%20with%20%60Fold%60%20%23333/near/193093958" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333.html#193093958">(Apr 06 2020 at 20:05)</a>:</h4>
<p>Heh, that's one of the questions I asked in the PR!</p>



<a name="193093991"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/introduce%20a%20%60Visit%60%20trait%20to%20go%20with%20%60Fold%60%20%23333/near/193093991" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333.html#193093991">(Apr 06 2020 at 20:05)</a>:</h4>
<p>I'd prefer to have relatively few callback sites, because otherwise every time we add any new type, we have to update the visitor, and I think very few visitors care about those little details</p>



<a name="193094019"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/introduce%20a%20%60Visit%60%20trait%20to%20go%20with%20%60Fold%60%20%23333/near/193094019" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333.html#193094019">(Apr 06 2020 at 20:05)</a>:</h4>
<p>But I see that <code>AliasEq</code> is a bit annoying!</p>



<a name="193094092"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/introduce%20a%20%60Visit%60%20trait%20to%20go%20with%20%60Fold%60%20%23333/near/193094092" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333.html#193094092">(Apr 06 2020 at 20:06)</a>:</h4>
<p>Keep in mind though that "higher-level callbacks" still allow you to intercept lower-level details (by matching down)</p>



<a name="193094158"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/introduce%20a%20%60Visit%60%20trait%20to%20go%20with%20%60Fold%60%20%23333/near/193094158" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/introduce.20a.20.60Visit.60.20trait.20to.20go.20with.20.60Fold.60.20.23333.html#193094158">(Apr 06 2020 at 20:06)</a>:</h4>
<p>I've been trying to figure out what is "bugging" me about AliasEq, I think it's that it's not a concept that comes "from Rust" somehow, it's more a detail in how we've defined things; this is not 100% true, since where clauses like <code>T: Foo&lt;Bar = Baz&gt;</code> are part of Rust</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>